package com.globallogic.vacationplanner.model.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.globallogic.vacationplanner.model.dao.declaration.IProjectDao;
import com.globallogic.vacationplanner.model.dao.declaration.IUserDao;
import com.globallogic.vacationplanner.model.entity.Project;
import com.globallogic.vacationplanner.model.entity.User;
import com.globallogic.vacationplanner.model.helper.DtoHelper;
import com.globallogic.vacationplanner.model.helper.ProjectDtoHelper;
import com.globallogic.vacationplanner.transfer.dto.impl.ProjectDto;
import com.globallogic.vacationplanner.transfer.dto.impl.UserDto;
import com.globallogic.vacationplanner.transfer.service.IProjectService;

@Service("projectService")
public class ProjectService implements IProjectService {
	private static final long serialVersionUID = 1L;
	private static Logger LOGGER = Logger.getLogger(ProjectService.class);
	private IUserDao userDao;
	private IProjectDao projectDao;

	public ProjectService() {
		LOGGER.info("Project service was created successfully");
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	@Transactional
	public void createProject(ProjectDto projectDto) {
		LOGGER.info("in createProject");
		User template = new User();
		template.setLogin(projectDto.getLoginCreator());
		User creator = userDao.read(template);
		Project project = new Project(creator, projectDto.getName(),
				projectDto.getDescription(), projectDto.getState());
		project.getMemberedUsers().add(creator);
		LOGGER.info("Add members to project");
		List<UserDto> userDtoList = projectDto.getUserMembersList();
		for (UserDto userDto : userDtoList) {
			template.setLogin(userDto.getLogin());
			User member = userDao.read(template);
			project.getMemberedUsers().add(member);
		}
		projectDao.insert(project);
		LOGGER.info("{Projects has been created");
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<UserDto> fillUsersList(UserDto userDto) {
		LOGGER.info("fillUsersList " + userDto.getLogin());
		User template = new User();
		template.setLogin(userDto.getLogin());
		User creator = userDao.read(template);
		List<User> users = projectDao.getUserListWithoutCreator(creator
				.getIdUser());
		return DtoHelper.convertUserList(users);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	@Transactional
	public void userLeaveProject(ProjectDto projectDto, UserDto userDto) {
		LOGGER.info("userLeaveProject " + projectDto.getIdProjects());
		User template = new User();
		template.setLogin(userDto.getLogin());
		Project project = projectDao.getProjectById(projectDto.getIdProjects());
		User user = userDao.read(template);
		project.getMemberedUsers().remove(user);
		projectDao.update(project);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public UserDto findUser(String login) {
		User template = new User();
		template.setLogin(login);
		User user = userDao.read(template);
		return DtoHelper.convertUserToUserDto(user);
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	@Transactional
	public List<ProjectDto> getUserMemberedProjects(UserDto user) {
		LOGGER.info("In getUserMemberedProjects" + user.getLogin());
		User template = new User();
		template.setLogin(user.getLogin());
		User creator = userDao.read(template);
		Set<Project> memberedProject = creator.getMemberedProjects();
		List<ProjectDto> projectDtoList = ProjectDtoHelper
				.getUserProjectDtoList(memberedProject);
		LOGGER.info("Getting user memberered projects is success "
				+ user.getLogin());
		return projectDtoList;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	@Transactional
	public List<ProjectDto> getUserOwnProjects(UserDto user) {
		LOGGER.info("In getUserOwnProjects" + user.getLogin());
		User template = new User();
		template.setLogin(user.getLogin());
		User creator = userDao.read(template);
		Set<Project> ownProject = creator.getOwnProjects();
		List<ProjectDto> projectDtoList = ProjectDtoHelper
				.getUserProjectDtoList(ownProject);
		LOGGER.info("Getting user own projects is success " + user.getLogin());
		return projectDtoList;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	@Transactional
	public List<ProjectDto> getAllProject() {
		LOGGER.info("getAllProject()");
		List<Project> projectsList = projectDao.readAll();
		List<ProjectDto> projectDtoList = ProjectDtoHelper
				.getProjectDtoList(projectsList);
		return projectDtoList;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	@Transactional
	public void edtiUserOwnProject(ProjectDto dto) {
		Project editProject = projectDao.getProjectById(dto.getIdProjects());
		editProject.setDescription(dto.getDescription());
		editProject.setName(dto.getName());
		editProject.setState(dto.getState());
		projectDao.update(editProject);
	}

	public UserDto getUserDto(String login) {
		User template = new User();
		template.setLogin(login);
		User user = userDao.read(template);
		return DtoHelper.convertUserToUserDto(user);
	}

	@Autowired
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	@Autowired
	public void setProjectDao(IProjectDao projectDao) {
		this.projectDao = projectDao;
	}
}